﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using System.Threading;
using bdnt_pets_system.Resources;
using System.Windows.Data;
using bdnt_pets_system.SystemServiceReference;
using System.Collections.ObjectModel;
using bdnt_pets_system.Tools;
using System.IO.IsolatedStorage;

namespace bdnt_pets_system.FrameViews
{
    public partial class FrameControl : UserControl
    {
        private IsolatedStorageSettings appSettings = null;
        SystemServiceReference.SystemServiceClient sysClient;
        SystemSetFrameControl cuSys;

        public FrameControl()
        {
            InitializeComponent();
            appSettings = IsolatedStorageSettings.ApplicationSettings;
            sysClient = new SystemServiceReference.SystemServiceClient();
            viewHd.Tag = gdMainConten;
            App.CurrentParentGrid = gdMainConten;
            InitEvent();
            gdMainConten.Tag = this;
        }

        private void InitEvent()
        {
            sysClient.GetAllMenuListByConditionCompleted += new EventHandler<GetAllMenuListByConditionCompletedEventArgs>(sysClient_GetAllMenuListByConditionCompleted);
            viewHd.rbZ.Checked += new RoutedEventHandler(rbZ_Checked);
            viewHd.rbY.Checked += new RoutedEventHandler(rbZ_Checked);
            viewHd.tbExit.Click += new RoutedEventHandler(tbExit_Click);
            if (appSettings.Contains(SessionManager.ISCHINESE))
            {
                var cul = bool.Parse(appSettings[SessionManager.ISCHINESE].ToString());
                if (cul)
                    viewHd.rbZ.IsChecked = true;
                else viewHd.rbY.IsChecked = true;
            }
        }

        void sysClient_GetAllMenuListByConditionCompleted(object sender, GetAllMenuListByConditionCompletedEventArgs e)
        {
            SessionManager.RoleAllMenus = e.Result;
            viewHd.InitMenus(SessionManager.RoleAllMenus, true);//加载头部菜单

            //加载系统设置页面
            cuSys = new SystemSetFrameControl();
            gdMainConten.Children.Clear();
            gdMainConten.Children.Add(cuSys);
        }

        void tbExit_Click(object sender, RoutedEventArgs e)
        {
            if (appSettings.Contains(SessionManager.USERINFO))
                appSettings.Remove(SessionManager.USERINFO);
            (this.Tag as MainPage).LoadPage();
        }

        void rbZ_Checked(object sender, RoutedEventArgs e)
        {
            string lanV = (sender as RadioButton).Tag.ToString();
            var strings = App.Current.Resources["lnCon"] as LanguageConverter;
            strings.ChangeCulture(lanV);

            viewHd.InitMenus(SessionManager.RoleAllMenus);
            if (cuSys != null)
            {
                cuSys.CuMenuId = -1;
                cuSys.InitMenus();
            }
        }

        //查询
        public void QueryData(bool fromLog = false)
        {
            if (fromLog)
            {
                if (appSettings.Contains(SessionManager.ISCHINESE))
                {
                    var cul = bool.Parse(appSettings[SessionManager.ISCHINESE].ToString());
                    if (cul)
                        viewHd.rbZ.IsChecked = true;
                    else viewHd.rbY.IsChecked = true;
                }
            }
            viewHd.tbCuName.Text = SessionManager.CurrentUser.EmpName;
            List<string> rs = new List<string>();
            foreach (var item in SessionManager.CurrentUser.RoleIds.Split(','))
            {
                int rsi;
                if (int.TryParse(item, out rsi))
                {
                    rs.Add(item);
                }
                //if (rsi != null)
                    //rs.Add(rsi);
            }
            sysClient.GetAllMenuListByConditionAsync(string.Join(",", rs));
        }
    }
}
